package com.xiaochong.tir.manager.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.tir.common.data.po.ExchangeInfo;
import com.xiaochong.tir.common.data.po.SyArea;
import com.xiaochong.tir.common.data.vo.ExchangeInfoVo;
import com.xiaochong.tir.common.data.vo.SysAreaVo;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.manager.service.ExchangeInfoService;
import com.xiaochong.tir.manager.service.SyAreaService;
import com.xiaochong.tir.manager.util.ManagerVersion;
import com.xiaochong.tir.manager.util.ReturnResult;
import com.xiaochong.tir.manager.util.SessionUtil;
import com.xiaochong.tir.manager.util.TreeBuilder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.*;

/**
 * Created by mxd on 2018/4/27.
 *交易所指数后台
 *
 */

@Controller
@Api(value = "交易所管理")
@RequestMapping(value = ManagerVersion.API_V1_PREFIX+"exchange/")
public class ExchangeController {

    @Autowired
    private ExchangeInfoService exchangeInfoService;
    @Autowired
    private SyAreaService syAreaService;
	@Autowired
	private RedisServer redisServer;

	/**
	 * 交易所指数查询
	 * @param exchangeInfo
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
    @ApiOperation(value = "交易所指数查询" , notes = "交易所指数查询")
    @PostMapping("listExchange")
    @ResponseBody
    public Object listExchange(ExchangeInfo exchangeInfo,
							   @RequestParam(value = "page")Integer pageIndex,
							   @RequestParam(value = "limit")Integer pageSize){
        PageInfo<ExchangeInfo> list = exchangeInfoService.selectByEntityList(exchangeInfo,pageIndex,pageSize);
        return new ReturnResult<ExchangeInfo>(0,"成功",list.getList(),list.getTotal());
    }

	/**
	 * 交易所指数新增
	 * @param exchangeInfo
	 * @return
	 */
	@ApiOperation(value = "交易所指数新增", notes = "交易所指数新增")
	@PostMapping("saveExchange")
	@ResponseBody
	public Object saveExchange(ExchangeInfo exchangeInfo) {
		redisServer.set(RedisKeysUtils.LOGINNAME,"交易所指数新增");
		try {
			ExchangeInfo info = exchangeInfoService.selectByEntity(exchangeInfo);
			if(null != info){
				return new ReturnResult<ExchangeInfo>(-1, "重复添加", 0, 0);
			}
			exchangeInfo.setDeleteFlag(false);
			exchangeInfo.setShelfFlag(true);
			exchangeInfo.setCreateTime(new Date());
			exchangeInfoService.insertSelective(exchangeInfo);
			//更新缓存
			List<ExchangeInfoVo> list = exchangeInfoService.selectExchangeList(new ExchangeInfoVo());
			redisServer.set(RedisKeysUtils.BOURSE_NAME_INDEX,JSON.toJSONString(list));
		} catch (Exception e) {
			e.printStackTrace();
			LogTrace.error(getClass().getSimpleName() + "/saveExchange", e.getMessage(), e);
			return new ReturnResult<ExchangeInfo>(-1, "新增失败", 0, 0);
		}
		return new ReturnResult<ExchangeInfo>(0, "新增成功", 1, 1);
	}


	/**
	 * 交易所指数修改
	 * @param exchangeInfo
	 * @return
	 */
	@ApiOperation(value = "交易所指数修改" , notes = "交易所指数修改")
    @PostMapping("updateExchange")
    @ResponseBody
    public Object updateExchange(ExchangeInfo exchangeInfo){
		redisServer.set(RedisKeysUtils.LOGINNAME,"交易所指数修改");
    	try{
			exchangeInfo.setUpdateTime(new Date());
			exchangeInfo.setShelfFlag(true);
			ExchangeInfo old = exchangeInfoService.selectByPrimaryKey(exchangeInfo.getId());
			exchangeInfoService.updateByPrimaryKey(exchangeInfo,old);
			//更新缓存
			List<ExchangeInfoVo> list = exchangeInfoService.selectExchangeList(new ExchangeInfoVo());
			redisServer.set(RedisKeysUtils.BOURSE_NAME_INDEX,JSON.toJSONString(list));
		} catch (Exception e) {
			e.printStackTrace();
			LogTrace.error(getClass().getSimpleName() + "/updateExchange", e.getMessage(), e);
			return new ReturnResult<ExchangeInfo>(-1, "保存失败", 0, 0);
		}
		return new ReturnResult<ExchangeInfo>(0, "保存成功", 1, 1);
	}

	/**
	 * 启用/停用
	 * @param id
	 * @param enableFlag
	 * @return
	 */
	@ApiOperation(value = "交易所状态修改" , notes = "交易所状态修改")
    @PostMapping("exchangeEnable")
    @ResponseBody
	public Object exchangeEnable(@RequestParam("id") Integer id, @RequestParam("enableFlag") Boolean enableFlag) {
		redisServer.set(RedisKeysUtils.LOGINNAME,"交易所指数启用/停用");
		try {
			ExchangeInfo exchangeInfo = new ExchangeInfo();
			exchangeInfo.setId(id);
			if (enableFlag .equals(true) ) {
				exchangeInfo.setEnableFlag(false);
				exchangeInfo.setShelfFlag(false);
			} else {
				exchangeInfo.setEnableFlag(true);
				exchangeInfo.setShelfFlag(true);
			}
			exchangeInfo.setUpdateTime(new Date());
			int res = exchangeInfoService.updateByPrimaryKey(exchangeInfo,exchangeInfoService.selectByPrimaryKey(id));
			//更新缓存
			List<ExchangeInfoVo> list = exchangeInfoService.selectExchangeList(new ExchangeInfoVo());
			redisServer.set(RedisKeysUtils.BOURSE_NAME_INDEX,JSON.toJSONString(list));
			return new ReturnResult<ExchangeInfo>(0, "保存成功", res, 1);
		} catch (Exception e) {
			e.printStackTrace();
			LogTrace.error(getClass().getSimpleName() + "/exchangeEnable", e.getMessage(), e);
			return new ReturnResult<ExchangeInfo>(-1, "保存失败", 0, 0);
		}
	}

	/**
	 * 交易所指数删除
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "交易所删除" , notes = "交易所删除")
    @PostMapping("deleteExchange")
    @ResponseBody
	public Object deleteExchange(@RequestParam("id") Integer id) {
		redisServer.set(RedisKeysUtils.LOGINNAME,"交易所指数删除");
		try {
			ExchangeInfo exchangeInfo = new ExchangeInfo();
			exchangeInfo.setId(id);
			exchangeInfo.setDeleteFlag(true);
            exchangeInfo.setUpdateTime(new Date());
			exchangeInfoService.updateByPrimaryKey(exchangeInfo,exchangeInfoService.selectByPrimaryKey(id));
			//更新缓存
			List<ExchangeInfoVo> list = exchangeInfoService.selectExchangeList(new ExchangeInfoVo());
			redisServer.set(RedisKeysUtils.BOURSE_NAME_INDEX,JSON.toJSONString(list));
		} catch (Exception e) {
			e.printStackTrace();
			LogTrace.error(getClass().getSimpleName() + "/deleteExchange", e.getMessage(), e);
			return new ReturnResult<ExchangeInfo>(-1, "删除失败", 0, 0);
		}
		return new ReturnResult<ExchangeInfo>(0, "删除成功", 1, 1);
	}

	/**
	 * 交易所指数列表页面
	 * @return
	 */
	@RequestMapping(value = "exchangeInfoList",method = RequestMethod.GET)
    public String exchangeInfoList(){
        return "/exchangeIndex/exchangeInfoList";
    }

	/**
	 * 交易所指数新增页面
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "exchangeInfoAdd",method = RequestMethod.GET)
    public String exchangeInfoAdd(Model model){
        return "/exchangeIndex/exchangeInfoAdd";
    }

	/**
	 * 交易所指数修改页面
	 * @param id
	 * @param model
	 * @return
	 */
    @RequestMapping(value = "exchangeInfoEdit",method = RequestMethod.GET)
    public String editExchPage(@RequestParam("id")Integer id,Model model){
    	ExchangeInfo exchangeInfo=exchangeInfoService.selectByPrimaryKey(id);
    	model.addAttribute("data", exchangeInfo);
    	//根据当前国家id查询国家id及父级id，返回id拼接字符串
		List<String> areaStr =syAreaService.selSelecdArea(exchangeInfo.getCountryId());
    	model.addAttribute("areaStr", areaStr.get(0));
        return "/exchangeIndex/exchangeInfoEdit";
    }

	/**
	 * 交易所交易对新增页面
	 * @param model
	 * @return
	 */
	@GetMapping("addtrapage")
	public String addTransactionPage(Model model){
		List<ExchangeInfo> exchangeInfo = exchangeInfoService.getExchangeInfo();
		model.addAttribute("exInfo",exchangeInfo);
		return "/exchangeIndex/cretransaction";
	}

	/**
	 * 无限极联动下拉框tree数据
	 * @return
	 */
	@ApiOperation(value = "查询地区",notes = "新增或修改交易所时选择所在地下拉框")
	@GetMapping("selSysArea")
	@ResponseBody
	public Object selSysArea(){
		List<SysAreaVo> treeNodes = syAreaService.selSysArea();
		List<SysAreaVo> trees = TreeBuilder.buildByRecursive(treeNodes);
		return  trees;
	}


}
